#include "stdafx.h"
#include "XMsgBox.h"

XMsgBox::XMsgBox(const SOUI::SStringW &caption, const SOUI::SStringW &title, const SOUI::SStringW &content,
                 const SOUI::SStringW &iconSkin, int mbType, bool centerAll)
    : XDlg(_T("LAYOUT:XML_X_MSGBOX")) {
    m_caption = caption;
    m_title = title;
    m_content = content;
    m_msgboxType = mbType;
    m_iconSkin = iconSkin;
    m_bCenterAll = centerAll;
}

XMsgBox::~XMsgBox() {
}

BOOL XMsgBox::OnInitDialog(HWND wndFocus, LPARAM lInitParam) {
    auto  msgCaption = FindChildByName("msgCaption");
    if (msgCaption != nullptr) {
        msgCaption->SetWindowText(m_caption);
    }
    
    auto  msgicon = FindChildByName("msgicon");
    if (msgicon != nullptr) {
        if (!m_iconSkin.IsEmpty()) {
            msgicon->SetAttribute(L"skin", m_iconSkin);
            msgicon->SetVisible(TRUE, TRUE);
        } else {
            msgicon->SetVisible(FALSE, TRUE);
        }
    }
    updateUI(m_title, m_content);
    
    auto btnSwitch = FindChildByName2<SOUI::STabCtrl>("btnSwitch");
    if (m_msgboxType == MB_OKCANCEL) {
        btnSwitch->SetCurSel(1);
        if (!m_buttonText1.IsEmpty()) {
            btnSwitch->GetPage(1)->FindChildByID(2)->SetWindowText(m_buttonText1);
        }
        if (!m_buttonText2.IsEmpty()) {
            btnSwitch->GetPage(1)->FindChildByID(1)->SetWindowText(m_buttonText2);
        }
    } else if (m_msgboxType == MB_YESNO) {
        btnSwitch->SetCurSel(2);
        if (!m_buttonText1.IsEmpty()) {
            btnSwitch->GetPage(2)->FindChildByID(7)->SetWindowText(m_buttonText1);
        }
        if (!m_buttonText2.IsEmpty()) {
            btnSwitch->GetPage(2)->FindChildByID(6)->SetWindowText(m_buttonText2);
        }
    } else if (m_msgboxType == MB_RETRYCANCEL) {
        btnSwitch->SetCurSel(3);
        if (!m_buttonText1.IsEmpty()) {
            btnSwitch->GetPage(2)->FindChildByID(2)->SetWindowText(m_buttonText1);
        }
        if (!m_buttonText2.IsEmpty()) {
            btnSwitch->GetPage(2)->FindChildByID(4)->SetWindowText(m_buttonText2);
        }
    } else {
        if (!m_buttonText1.IsEmpty()) {
            btnSwitch->GetPage(0)->FindChildByID(1)->SetWindowText(m_buttonText1);
        }
    }
    
    return 0;
}

void XMsgBox::OnBtnClick(int uID) {
    EndDialog(uID);
}

void XMsgBox::SetButtonText(const SOUI::SStringW &buttonText1, const SOUI::SStringW &buttonText2) {
    m_buttonText1 = buttonText1;
    m_buttonText2 = buttonText2;
}

void XMsgBox::updateUI(const SOUI::SStringW &title, const SOUI::SStringW &content) {
    m_title = title;
    m_content = content;
    
    if (IsWindow()) {
        auto  msgTitle = FindChildByName("msgTitle");
        if (msgTitle != nullptr) {
            if (m_bCenterAll) {
                msgTitle->SetAttribute(L"align", L"center");
            }
            msgTitle->SetWindowText(m_title);
            if (m_title.IsEmpty()) {
                msgTitle->SetVisible(FALSE, TRUE);
            }
        }
        
        auto  msgContent = FindChildByName("msgContent");
        if (msgContent != nullptr) {
            if (m_bCenterAll) {
                msgContent->SetAttribute(L"align", L"center");
            }
            
            msgContent->SetWindowText(m_content);
            if (m_content.IsEmpty()) {
                msgContent->SetVisible(FALSE, TRUE);
            }
        }
    }
}
